<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="选择仓库" prop="storage">
        <el-select v-model="queryParams.storage" placeholder="存放仓库" clearable style="width: 190px;">
          <el-option v-for="item in storageList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="选择分类" prop="model">
        <el-select v-model="queryParams.goodsType" placeholder="物品分类" clearable style="width: 190px;">
          <el-option v-for="item in goodsTypeList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="货名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入货名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['wh:goods:add']">入库</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['wh:goods:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wh:goods:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="仓库" align="center" prop="storageName" />
      <el-table-column label="分类" align="center" prop="goodsTypeName" />
      <el-table-column label="货名" align="center" prop="name" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="数量" align="center" prop="count" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleInPut(scope.row)"
            v-hasPermi="['wh:goods:edit']">入库</el-button>
          <el-button link type="primary" icon="Edit" @click="handleOutPut(scope.row)"
            v-hasPermi="['wh:goods:edit']">出库</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['wh:goods:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改物品对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="goodsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择仓库" prop="storage">
          <el-select v-model="form.storage" placeholder="存放仓库" clearable>
            <el-option v-for="item in storageList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="选择分类" prop="goodsType">
          <el-select v-model="form.goodsType" placeholder="物品分类" clearable>
            <el-option v-for="item in goodsTypeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="货名" prop="name">
          <el-input v-model="form.name" placeholder="请输入货名" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="数量" prop="count">
          <el-input v-model="form.count" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 添加或修改物品对话框 -->
    <el-dialog title="入库" v-model="inOpen" width="500px" append-to-body>
      <el-form ref="goodsInRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库" prop="storage">
          <span>{{ form.storageName }}</span>
        </el-form-item>
        <el-form-item label="分类" prop="goodsType">
          <span>{{ form.goodsTypeName }}</span>
        </el-form-item>
        <el-form-item label="货名" prop="name">
          <span>{{ form.name }}</span>
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <span>{{ form.model }}</span>
        </el-form-item>

        <el-form-item label="入库数量" prop="count">
          <el-input v-model="form.count" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="inPutSubmit">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 出库 -->
    <el-dialog title="出库" v-model="outOpen" width="500px" append-to-body>
      <el-form ref="goodsInRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库" prop="storage">
          <span>{{ form.storageName }}</span>
        </el-form-item>
        <el-form-item label="分类" prop="goodsType">
          <span>{{ form.goodsTypeName }}</span>
        </el-form-item>
        <el-form-item label="货名" prop="name">
          <span>{{ form.name }}</span>
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <span>{{ form.model }}</span>
        </el-form-item>

        <el-form-item label="出库数量" prop="count">
          <el-input v-model="form.count" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="outSubmit">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Goods">
import { listGoods, getGoods, delGoods, addGoods, updateGoods, inPutGoods, outPutGoods } from "@/api/wh/goods";

import { listGoodstype } from "@/api/wh/goodstype";

import { listStorage } from "@/api/wh/storage";


const { proxy } = getCurrentInstance();

const goodsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
//
const goodsTypeList = ref([]);

const storageList = ref([]);
//
const inOpen = ref(false);
const outOpen = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    model: null,
    storage: null,
    goodsType: null,
  },
  rules: {
    name: [
      { required: true, message: "货名不能为空", trigger: "blur" }
    ],
    storage: [
      { required: true, message: "仓库id不能为空", trigger: "blur" }
    ],
    goodsType: [
      { required: true, message: "分类id不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询物品列表 */
function getList() {
  loading.value = true;
  listGoods(queryParams.value).then(response => {
    goodsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询物品类型列表 */
function getGoodTypeList() {
  let typeParams = {
    pageNum: 1,
    pageSize: 100,
  }
  listGoodstype(typeParams).then(response => {
    goodsTypeList.value = response.rows;
  });
}

/** 查询仓库列表 */
function getStorageList() {
  let params = {
    pageNum: 1,
    pageSize: 100,
  }
  listStorage(params).then(response => {
    storageList.value = response.rows;
  });
}



// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    model: null,
    storage: null,
    goodsType: null,
    count: null,
    remark: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null
  };
  proxy.resetForm("goodsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加物品";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getGoods(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改物品";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["goodsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateGoods(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGoods(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除物品编号为"' + _ids + '"的数据项？').then(function () {
    return delGoods(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

//============================================================
/** 修改按钮操作 */
function handleInPut(row) {
  reset();
  const _id = row.id || ids.value
  getGoods(_id).then(response => {
    form.value = response.data;
    inOpen.value = true;
  });
}

/** 提交按钮 */
function inPutSubmit() {
  inPutGoods(form.value).then(response => {
    proxy.$modal.msgSuccess("入库成功");
    inOpen.value = false;
    getList();
  });
}

//============================================================
/** 出库 */
function handleOutPut(row) {
  reset();
  const _id = row.id || ids.value
  getGoods(_id).then(response => {
    form.value = response.data;
    outOpen.value = true;
  });
}

/** 提交按钮 */
function outSubmit() {
  outPutGoods(form.value).then(response => {
    proxy.$modal.msgSuccess("出库成功");
    outOpen.value = false;
    getList();
  });
}


getList();
getGoodTypeList();
getStorageList();
</script>
